#!/bin/sh /etc/rc.common
 
USE_PROCD=1
 
START=20
STOP=01

#BINLOADER_BIN="/usr/bin/AdGuardHome/AdGuardHome -c /etc/AdGuardHome/AdGuardHome.yaml"
BINLOADER_BIN="/etc/AdGuardHome/startAdGuardHome.sh"
CONFIGURATION=AdGuardHome

service_triggers()
{
    procd_add_reload_trigger "CONFIGURATION"
}

reload_service()
{
	stop
    start
}

start_service() {
    # Reading config
    config_load "${CONFIGURATION}"
    local enabled
 
   config_get_bool enabled AdGuardHome enabled 0
	
	if [ "$enabled" -eq 0 ]; then
		echo "AdGuardHome has turned off"
		return 1
	fi
	
	procd_open_instance
	procd_set_param respawn
	# pass config to script on start
	procd_set_param command $BINLOADER_BIN
	procd_close_instance
	echo "AdGuardHome turn on"
	echo "enabled="$enabled""
}

stop_service()
{
    config_load "${CONFIGURATION}"
    local enabled
 
    config_get_bool enabled AdGuardHome enabled 0
	killall -q AdGuardHome
	echo "AdGuardHome turn off"
	echo "enabled="$enabled""
}
